var _deepCopyAll = function _deepCopyAll(source) {
	if (typeof source != 'object') {
		return source;
	}
	if (Array.isArray(source)) {
		return source.concat();
	}
	var target = {};
	for (var s in source) {
		if (Array.isArray(source[s])) {
			target[s] = source[s].concat();
		} else if (!Array.isArray(source[s]) && typeof source[s] == 'object') {
			// target[s] = arguments.callee(source[s]);
			target[s] = _deepCopyAll(source[s]);
		} else {
			target[s] = source[s];
		}
	}
	return target;
}

exports.isNullObj = function(obj) {
	if (typeof obj == 'object') {
		for (var o in obj) {
			return false
		}
		return true
	}
	return true;
}
exports.deepCopy = function(source, options) {
	if (Array.isArray(source)) {
		return source.concat();
	}
	var target = {},
		copy = options && Array.isArray(options.copy) && options.copy || false,
		uncopy = options && Array.isArray(options.uncopy) && options.uncopy || false;
	source = typeof source == 'object' ? source : {};
	if (copy) {
		for (var i = 0; i < copy.length; i++) {
			var ele = copy[i];
			if (ele in source) {
				target[ele] = _deepCopyAll(source[ele]);
			}
		}
		return target;
	} else if (uncopy) {
		target = _deepCopyAll(source)
		while (Array.isArray(uncopy) && uncopy.length > 0) {
			delete target[uncopy.shift()];
		}
		return target;
	} else {
		return _deepCopyAll(source);
	}
}

exports.merge = function(target, source) {
	var name = '_def',
		obj = {};
	if (arguments.length == 1) {
		return _deepCopyAll(arguments[0]);
	}
	if (typeof target != 'object') {
		name = target.name || name;
		return obj[name] = target;
	}
	if (typeof source != 'object') {
		name = source.name || name;
		return target[name] = source;
	}
	for (var s in source) {
		target[s] = source[s];
	}
	return target;
}

var parseTimeConf = exports.parseTimeConf = function(time, ms) {
	var t = 3600;
	if (typeof time === 'number' && time > 0) {
		return ms == 1 ? time * 1000 : time;
	}

	if (time.indexOf('sec') > -1) {
		t = time.split("sec")[0]
	} else if (time.indexOf('min') > -1) {
		t = parseInt(time.split("min")[0]) * 60;
	} else if (time.indexOf('hou') > -1) {
		t = parseInt(time.split("hou")[0]) * 3600;
	} else if (time.indexOf('day') > -1) {
		t = parseInt(time.split("day")[0]) * 86400;
	} else if (time.indexOf('s') > -1) {
		t = time.split("s")[0]
	} else if (time.indexOf('m') > -1) {
		t = parseInt(time.split("m")[0]) * 60;
	} else if (time.indexOf('h') > -1) {
		t = parseInt(time.split("h")[0]) * 3600;
	} else if (time.indexOf('d') > -1) {
		t = parseInt(time.split("d")[0]) * 86400;
	} else if (parseInt(time) == 0) {
		t = parseInt(time);
	}
	if (ms && ms == 1) {
		t *= 1000
	}
	return t;
}

exports.trim = function(str) {
	if (typeof str != 'string') {
		str = new String(str);
	}
	return str.replace(/(^\s*)|(\s*$)/g, "");
}

exports.parseUsername = function(username) {
	if (!username)
		return false;
	return true;
}

exports.fromMobile = function(req) {
	var qs = req.query;
	return global.sysConfig.channelids[qs.channelid].type == 1
}

exports.validate = {
	trim: this.trim
}
exports.validate.Mobile = function(mobileNumber) {
	mobileNumber = this.trim(mobileNumber)
	var phoneReg = /^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
	if (!phoneReg.test(mobileNumber)) {
		return false;
	}
	return true;
}

exports.validate.IdCard = function(idCard) {
	idCard = this.trim(idCard);
	//15位和18位身份证号码的正则表达式
	var regIdCard = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
	//如果通过该验证，说明身份证格式正确，但准确性还需计算
	if (regIdCard.test(idCard)) {
		if (idCard.length == 15) {
			//将15位身份证转成18位
			//校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
			var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
			var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
			var nTemp = 0,
				i;
			idCard = idCard.substr(0, 6) + '19' + idCard.substr(6, idCard.length - 6);
			for (i = 0; i < 17; i++) {
				nTemp += idCard.substr(i, 1) * arrInt[i];
			}
			idCard += arrCh[nTemp % 11];
		}
		if (idCard.length == 18) {
			var idCardWi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); //将前17位加权因子保存在数组里
			var idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2); //这是除以11后，可能产生的11位余数、验证码，也保存成数组
			var idCardWiSum = 0; //用来保存前17位各自乖以加权因子后的总和
			for (var i = 0; i < 17; i++) {
				idCardWiSum += idCard.substring(i, i + 1) * idCardWi[i];
			}
			var idCardMod = idCardWiSum % 11; //计算出校验码所在数组的位置
			var idCardLast = idCard.substring(17); //得到最后一位身份证号码
			//如果等于2，则说明校验码是10，身份证号码最后一位应该是X
			//用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
			var istrue = ((idCardMod == 2) && idCardLast.toLocaleLowerCase() == 'x') || (idCardLast == idCardY[idCardMod]);
			if (istrue) {
				return true;
			}
		}
	}
	return false;
}

exports.validate.BankNo = function(cardno) {
	bankno = this.trim(cardno)
	var lastNum = bankno.substr(bankno.length - 1, 1); //取出最后一位（与luhm进行比较）
	var first15Num = bankno.substr(0, bankno.length - 1); //前15或18位
	var newArr = new Array();
	for (var i = first15Num.length - 1; i > -1; i--) { //前15或18位倒序存进数组
		newArr.push(first15Num.substr(i, 1));
	}
	var arrJiShu = new Array(); //奇数位*2的积 <9
	var arrJiShu2 = new Array(); //奇数位*2的积 >9
	var arrOuShu = new Array(); //偶数位数组
	for (var j = 0; j < newArr.length; j++) {
		if ((j + 1) % 2 == 1) { //奇数位
			if (parseInt(newArr[j]) * 2 < 9)
				arrJiShu.push(parseInt(newArr[j]) * 2);
			else
				arrJiShu2.push(parseInt(newArr[j]) * 2);
		} else //偶数位
			arrOuShu.push(newArr[j]);
	}
	var jishu_child1 = new Array(); //奇数位*2 >9 的分割之后的数组个位数
	var jishu_child2 = new Array(); //奇数位*2 >9 的分割之后的数组十位数
	for (var h = 0; h < arrJiShu2.length; h++) {
		jishu_child1.push(parseInt(arrJiShu2[h]) % 10);
		jishu_child2.push(parseInt(arrJiShu2[h]) / 10);
	}

	var sumJiShu = 0; //奇数位*2 < 9 的数组之和
	var sumOuShu = 0; //偶数位数组之和
	var sumJiShuChild1 = 0; //奇数位*2 >9 的分割之后的数组个位数之和
	var sumJiShuChild2 = 0; //奇数位*2 >9 的分割之后的数组十位数之和
	var sumTotal = 0;
	for (var m = 0; m < arrJiShu.length; m++) {
		sumJiShu = sumJiShu + parseInt(arrJiShu[m]);
	}

	for (var n = 0; n < arrOuShu.length; n++) {
		sumOuShu = sumOuShu + parseInt(arrOuShu[n]);
	}

	for (var p = 0; p < jishu_child1.length; p++) {
		sumJiShuChild1 = sumJiShuChild1 + parseInt(jishu_child1[p]);
		sumJiShuChild2 = sumJiShuChild2 + parseInt(jishu_child2[p]);
	}
	//计算总和
	sumTotal = parseInt(sumJiShu) + parseInt(sumOuShu) + parseInt(sumJiShuChild1) + parseInt(sumJiShuChild2);
	//计算Luhm值
	var k = parseInt(sumTotal) % 10 == 0 ? 10 : parseInt(sumTotal) % 10;
	var luhm = 10 - k;
	if (lastNum && lastNum == luhm) {
		return true;
	}
	return false;
}

exports.validate.Name = function(name) {
	name = this.trim(name)
	var nameReg = /^[\u4e00-\u9fa5]+$/;
	if (nameReg.test(name)) {
		return true;
	}
	return false;
}

exports.week = function(datetime) {
	var dt = new Date(datetime),
		week = '';
	if (!dt) {
		dt = new Date()
	}
	switch (dt.getDay()) {
		case 0:
			week = "周日";
			break;
		case 1:
			week = "周一";
			break;
		case 2:
			week = "周二";
			break;
		case 3:
			week = "周三";
			break;
		case 4:
			week = "周四";
			break;
		case 5:
			week = "周五";
			break;
		case 6:
			week = "周六";
			break;
	}
	return week;
}